package com.pocketsweet.ui.uilib;

import com.pocketsweet.R;
import com.pocketsweet.ui.Main;
import com.pocketsweet.ui.MyLike;
import com.pocketsweet.ui.TempMain;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HeaderLayout extends RelativeLayout {

	private Context ctx;
	private LayoutInflater mInflater;
	private View mHeader;
	// 左侧
	public LinearLayout llLeftContainer;
	public RelativeLayout relBg;
	ImageView ivLeftImg;
	TextView tvLeftText;
	private onLeftContainerListener mLeftContainerClickListener;

	// 中部
	public LinearLayout llMiddleContainer;
	TextView tvMiddleText;
	LinearLayout llTab;
	ImageView cursor;
	private int offsetone = 0;// 动画图片偏移量
	private int offsetOneToTwo = 0;// 滑动距离
	public static int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	private Animation animation = null;
	TextView tvNewOnline;
	TextView tvRecomend;
	// 右侧
	public LinearLayout llRightContainer;
	ImageView ivRightImg;
	static ImageView circleRecomend;
	TextView tvRightText;
	private onRightContainerListener mRightContainerClickListener;

	public HeaderLayout(Context context) {
		super(context);
		init(context);
	}

	public HeaderLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		mInflater = LayoutInflater.from(context);
		mHeader = mInflater.inflate(R.layout.view_header, null);
		addView(mHeader);
		initViews();
	}

	private void initViews() {
		llLeftContainer = (LinearLayout) findViewById(R.id.llLeftContainer);
		ivLeftImg = (ImageView) findViewById(R.id.ivLeftImg);
		tvLeftText = (TextView) findViewById(R.id.tvLeftText);
		llMiddleContainer = (LinearLayout) findViewById(R.id.llMiddleContainer);
		relBg = (RelativeLayout) findViewById(R.id.relBg);
		tvMiddleText = (TextView) findViewById(R.id.tvMiddleText);
		llRightContainer = (LinearLayout) findViewById(R.id.llRightContainer);
		ivRightImg = (ImageView) findViewById(R.id.ivRightImg);
		circleRecomend = (ImageView) findViewById(R.id.circleRecomend);
		tvRightText = (TextView) findViewById(R.id.tvRightText);
		llTab = (LinearLayout) findViewById(R.id.llTab);
		llLeftContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mLeftContainerClickListener != null) {
					mLeftContainerClickListener.onClick();
				}
			}
		});
		llRightContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mRightContainerClickListener != null) {
					mRightContainerClickListener.onClick();
				}
			}
		});
	}

	public void setLeftContentShow(int show) {
		llLeftContainer.setVisibility(show);
	}

	public void setLeftText(String value) {
		tvLeftText.setText(value);
	}

	public void setLeftImg(int value) {
		ivLeftImg.setImageDrawable(getResources().getDrawable(value));
	};

	public void setRightContentShow(int show) {
		llRightContainer.setVisibility(show);
	}

	public void setRightText(String value) {
		tvRightText.setText(value);
	}

	public void setRightImg(int value) {
		ivRightImg.setImageDrawable(getResources().getDrawable(value));
	};

	public void setRelBgAlpha() {
		relBg.setBackgroundColor(getResources().getColor(
				R.color.profile_action_bar_bg));
		relBg.getBackground().setAlpha(0);
	};
	public void setRelBgBlack() {
		relBg.setBackgroundColor(getResources().getColor(
				R.color.main_actionBar_color));
	};

	public void setMiddleText(String value) {
		tvMiddleText.setText(value);
	}

	public static void setRecommendCircle() {
		circleRecomend.setVisibility(View.VISIBLE);
	}
	public static void setRecommendCircleGone() {
		circleRecomend.setVisibility(View.INVISIBLE);
	}
	
	public ImageView getIvRightImg() {
		return ivRightImg;
	}

	public void init(HeaderStyle style) {
		llTab.setVisibility(View.GONE);
		tvMiddleText.setVisibility(View.VISIBLE);
		switch (style) {
		case TEXT_TITLE_TEXT:
			ivLeftImg.setVisibility(View.GONE);
			tvLeftText.setVisibility(View.VISIBLE);
			ivRightImg.setVisibility(View.GONE);
			tvRightText.setVisibility(View.VISIBLE);
			break;

		case IMG_TITLE_TEXT:
			ivLeftImg.setVisibility(View.VISIBLE);
			tvLeftText.setVisibility(View.GONE);
			ivRightImg.setVisibility(View.GONE);
			tvRightText.setVisibility(View.VISIBLE);
			break;

		case TEXT_TITLE_IMG:
			ivLeftImg.setVisibility(View.GONE);
			tvLeftText.setVisibility(View.VISIBLE);
			ivRightImg.setVisibility(View.VISIBLE);
			tvRightText.setVisibility(View.GONE);
			break;

		case IMG_TITLE_IMG:
			ivLeftImg.setVisibility(View.VISIBLE);
			tvLeftText.setVisibility(View.GONE);
			ivRightImg.setVisibility(View.VISIBLE);
			tvRightText.setVisibility(View.GONE);
			break;
		}
	}

	public void InitcursorView() {
		llTab.setVisibility(View.VISIBLE);
		tvMiddleText.setVisibility(View.GONE);

		tvNewOnline = (TextView) findViewById(R.id.tvNewOnline);
		tvRecomend = (TextView) findViewById(R.id.tvRecomend);

		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(),
				R.drawable.tab_bottom_short).getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		Main.getInstance().getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度

		offsetone = (250 * dm.densityDpi / 160 / 2 - bmpW * 106 / 100) / 2;// 计算偏移量rate=((density/baseDensity)*dp)/x;我们把所期待的比例记为rate,baseDensity=160，屏幕的宽度（像素数）为x，屏幕密度为density
		offsetOneToTwo = offsetone * 2 + bmpW * 110 / 100;
		Matrix matrix = new Matrix();
		matrix.postTranslate(offsetone, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}
	
	public void InitcursorView(boolean isMain) {
		llTab.setVisibility(View.VISIBLE);
		tvMiddleText.setVisibility(View.GONE);
		
		tvNewOnline = (TextView) findViewById(R.id.tvNewOnline);
		tvRecomend = (TextView) findViewById(R.id.tvRecomend);
		
		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(),
				R.drawable.tab_bottom_short).getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		MyLike.getInstance().getWindowManager().getDefaultDisplay()
		.getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		
		offsetone = (250 * dm.densityDpi / 160 / 2 - bmpW * 106 / 100) / 2;// 计算偏移量rate=((density/baseDensity)*dp)/x;我们把所期待的比例记为rate,baseDensity=160，屏幕的宽度（像素数）为x，屏幕密度为density
		offsetOneToTwo = offsetone * 2 + bmpW * 110 / 100;
		Matrix matrix = new Matrix();
		matrix.postTranslate(offsetone, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}
	
	public void Initcursor() {
		llTab.setVisibility(View.VISIBLE);
		tvMiddleText.setVisibility(View.GONE);

		tvNewOnline = (TextView) findViewById(R.id.tvNewOnline);
		tvRecomend = (TextView) findViewById(R.id.tvRecomend);

		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(),
				R.drawable.tab_bottom_short).getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		TempMain.getInstance().getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度

		offsetone = (250 * dm.densityDpi / 160 / 2 - bmpW * 106 / 100) / 2;// 计算偏移量rate=((density/baseDensity)*dp)/x;我们把所期待的比例记为rate,baseDensity=160，屏幕的宽度（像素数）为x，屏幕密度为density
		offsetOneToTwo = offsetone * 2 + bmpW * 110 / 100;
		Matrix matrix = new Matrix();
		matrix.postTranslate(offsetone, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}
	
	public void setTvNewOnline(String tv){
		tvNewOnline.setText(tv);
	}
	
	public void setTvRecomend(String tv){
		tvRecomend.setText(tv);
	}

	public void slidleft() {
		animation = new TranslateAnimation(offsetOneToTwo, 0, 0, 0);
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(300);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				tvNewOnline
						.setTextColor(getResources().getColor(R.color.white));
				tvRecomend.setTextColor(getResources().getColor(
						R.color.text_999));
			}
		});
		cursor.startAnimation(animation);
	}

	public void slidRight() {
		animation = new TranslateAnimation(offsetone, offsetOneToTwo, 0, 0);
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(300);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				tvNewOnline.setTextColor(getResources().getColor(
						R.color.text_999));
				tvRecomend.setTextColor(getResources().getColor(R.color.white));

			}
		});
		cursor.startAnimation(animation);
	}

	public enum HeaderStyle {
		TEXT_TITLE_TEXT, IMG_TITLE_TEXT, TEXT_TITLE_IMG, IMG_TITLE_IMG;
	}

	public void setLeftContainerClickListener(onLeftContainerListener listener) {
		mLeftContainerClickListener = listener;
	}

	public interface onLeftContainerListener {
		void onClick();
	}

	public void setRighttContainerClickListener(
			onRightContainerListener listener) {
		mRightContainerClickListener = listener;
	}

	public interface onRightContainerListener {
		void onClick();
	}
}
